package sites.manager.admin.site;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import sites.manager.common.kit.Ret;
import sites.manager.common.kit.SiteInfo;
import sites.manager.common.kit.StrKit;
import sites.manager.common.mapper.SiteMapper;
import sites.manager.common.model.Site;

import java.util.ArrayList;

@Component
public class SiteService extends ServiceImpl<SiteMapper, Site> {
	
	@Autowired
	private SiteMapper mapper;

	/**
	 * 保存站点
	 *
	 * @param site
	 * @return
	 */
	public Ret saveSite(Site site) {
		Ret ret = Ret.ok();

		Site temp = getBySiteSign(site.getSiteSign());
		if(temp != null){
			ret.setFail();
			ret.setMsg("站点标示【"+site.getSiteSign()+"】已被占用，请设定其他值！");
			return ret;
		}

		site.setStatus(SiteInfo.statusDel);
		boolean flag = site.insert();
		if(!flag) {
			ret.setFail();
			ret.setMsg("保存失败");
		}
		return ret;
	}

	/**
	 * 分页查询
	 *
	 * @param site
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public Ret getSites(Site site, int pageNum, int pageSize) {
		Ret ret = Ret.ok();
		
		//构建查询条件
		LambdaQueryWrapper<Site> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(StrKit.notBlank(site.getSiteName()), Site::getSiteName, site.getSiteName())
			.like(StrKit.notBlank(site.getSiteSign()), Site::getSiteSign, site.getSiteSign())
			.like(StrKit.notBlank(site.getSiteUrl()), Site::getSiteUrl, site.getSiteUrl());
		
		//进行分页查询
		Page<Site> page = new Page<>(pageNum, pageSize);
		Page<Site> sitePage = page(page, wrapper);
		
		//组装结果
		ret.setCode("0");
		ret.setData(sitePage.getRecords());
		ret.setCount((int)count(wrapper));
		
		return ret;
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	public Ret deleteSite(String ids){
		Ret ret = Ret.create();

		String[] idArray = ids.split(",");

		ArrayList<Long> deleteIds = new ArrayList<>();
		for(int i=0,j=idArray.length; i<j; i++){
			deleteIds.add(Long.parseLong(idArray[i]));
		}

		int num = mapper.deleteBatchIds(deleteIds);

		if(num > 0){
			ret.setOk();
			ret.setMsg("删除"+num+"行");
		} else {
			ret.setFail();
			ret.setMsg("操作失败");
		}
		return ret;
	}

	/**
	 * 根据id获取site
	 *
	 * @param id
	 * @return
	 */
	public Site getSiteById(String id){
		long siteId = Long.parseLong(id);
		return getById(siteId);
	}

	/**
	 * 更新
	 *
	 * @param site
	 * @return
	 */
	public Ret updateSite(Site site){
		Ret ret = Ret.ok();
		boolean flag = updateById(site);
		if(!flag) {
			ret.setFail();
			ret.setMsg("保存失败");
		}
		return ret;
	}

	/**
	 * 设置当前站点
	 *
	 * @param id
	 * @return
	 */
	public Ret resetSite(String id){
		Ret ret = Ret.create();
		int m = mapper.updateSite2Del();

		int n = mapper.updateSite2Normal(Long.parseLong(id));
		if(m>0 && n>0) {
			ret.setOk();
			ret.setMsg("重置成功");
		} else {
			ret.setFail();
			ret.setMsg("重置失败");
		}
		return ret;
	}

	/**
	 * 根据站点标示获取站点信息
	 *
	 * @param siteSign
	 * @return
	 */
	public Site getBySiteSign(String siteSign){
		if(StrKit.isBlank(siteSign)){
			return null;
		}

		LambdaQueryWrapper<Site> wrapper = new LambdaQueryWrapper<Site>();
		wrapper.eq(StrKit.notBlank(siteSign), Site::getSiteSign, siteSign);

		return getOne(wrapper);
	}

	/**
	 * 查询主站点
	 *
	 * @return
	 */
	public Site getMainSite(){
		LambdaQueryWrapper<Site> wrapper = new LambdaQueryWrapper<Site>();
		wrapper.eq(Site::getStatus, SiteInfo.statusNormal);
		wrapper.orderByDesc(Site::getUpdateTime);
		return getOne(wrapper);
	}

}
